/**
 * 项目名称: Topic_445.cpp
 * 项目路径: study/DataStructure/LinkedList/LeetCode/链表/中等
 * 编辑时间: 2024年06月28日21时11分42秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode * result(ListNode * head,ListNode * tmp,ListNode * tail){
        while (tmp){
            tail = tmp->next;
            tmp->next = head;
            head = tmp;
            tmp = tail;
        }
        return head;
    }
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if(!l1 && !l2)return nullptr;

        ListNode * tmp_l2 = l2 , * tmp_l1 = l1;

        ListNode * reverse_l1 = result(nullptr,tmp_l1, nullptr);
        ListNode * reverse_l2 = result(nullptr,tmp_l2, nullptr);

        ListNode * ans = nullptr;

        int cnt = 0;
        while (reverse_l1 || reverse_l2){
            int a = 0;int b = 0;
            if(reverse_l1){
                a=reverse_l1->val;
                reverse_l1 = reverse_l1->next;
            }
            if(reverse_l2){
                b=reverse_l2->val;
                reverse_l2 = reverse_l2->next;
            }
            ListNode * tmp = new ListNode;
            tmp->val = (a + b + cnt)%10;
            tmp->next = ans;
            ans = tmp;
            cnt = (a + b + cnt)/10;
        }
        if(cnt){
            ListNode * tmp = new ListNode;
            tmp->val = 1;
            tmp->next = ans;
            ans = tmp;
        }
        return ans;
    }
};

int main() {

    return 0;
}
